package com.ruoyi.rsikManage.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.rsikManage.domain.TUnitConfig;
import com.ruoyi.rsikManage.domain.common.SubscriptionsRequest;
import com.ruoyi.rsikManage.domain.vo.DiagnosisPlanRequestBody;
import com.ruoyi.rsikManage.domain.vo.StreamRequest;
import com.ruoyi.rsikManage.service.ITUnitConfigService;
import com.ruoyi.rsikManage.service.JyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * @author [作者名]
 * @description [极云公共方法]
 * @date [2024-03-20]
 */
@RequestMapping("/visual/platform")
@RestController
public class JyController extends BaseController {
    @Autowired
    JyService jyService;

    @Value("${jy.switch}")
    private String JY_SWITCH;

    @Autowired
    private ITUnitConfigService iTUnitConfigService;

    @GetMapping("getHttpHeader")
    AjaxResult getHttpHeader() {
        HashMap<Object, Object> res = new HashMap<>();
        res.put("HttpRequestHeaders", jyService.getHttpHeader());
        return AjaxResult.success(res);
    }

    /**
     * 获取免密登录 url
     *
     */
    @PostMapping("password-free-login-url")
    public AjaxResult getPasswordFreeLoginURL(RestTemplate restTemplate)
    {
        return jyService.getPasswordFreeLoginURL(restTemplate);
    }

    /**
     * 获取免密登录 url 集团版本
     *
     */
    @RequestMapping(value = "/password-free-login-url/group", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult getPasswordFreeLoginURLV2(@RequestBody(required = false)TUnitConfig tUnitConfig)
    {
        return jyService.getPasswordFreeLoginURLForUnitCodes(tUnitConfig);
    }
    /**
     * 获取接口认证token /visual/platform/getAccessToken
     */
    @GetMapping("/getAccessToken")
    public String getAccessToken() {
        return jyService.getAccessToken();
    }
    /**
     * 刷新token
     */
    @GetMapping("/refreshToken")
    public String refreshToken() {
        return jyService.getNewAccessToken();
    }
    /**
     * 实时事件订阅
     */
    @PostMapping("/eventSubscriptions")
    public String eventSubscriptions(@RequestBody Map<String,Object> param) {
        return jyService.eventSubscriptions(param);
    }

    /**
     * 实时事件订阅
     * 修改时间 24/10/25
     * 修改内容 新增权限管理-数据管理
     */
    @PostMapping("/eventSubscriptions/insert")
    public String eventSubscriptionsForUnitCodes(@RequestBody Map<String,Object> param) {

        Long unitCode = (Long)param.get("unitCode");
        TUnitConfig tUnitConfigRequest = new TUnitConfig();
        tUnitConfigRequest.setUnitCode(unitCode);
        List<TUnitConfig> tUnitConfigs = iTUnitConfigService.selectTUnitConfigList(tUnitConfigRequest);
        TUnitConfig tUnitConfig = tUnitConfigs.get(0);
        return jyService.eventSubscriptionsForUnitCodes(tUnitConfig,param);
    }

    /**
     * 查看订阅事件
     */
    @GetMapping("/eventSubscriptions")
    public String selectEventSubscriptions() {
        return jyService.selectEventSubscriptions();
    }

    /**
     * 查看订阅事件
     */
    @PostMapping("/eventSubscriptions/list")
    public String selectEventSubscriptionsForUnitCodes(@RequestBody SubscriptionsRequest subscriptionsRequest) {
        Long unitCode = subscriptionsRequest.getUnitCode();
        TUnitConfig tUnitConfigRequest = new TUnitConfig();
        tUnitConfigRequest.setUnitCode(unitCode);
        List<TUnitConfig> tUnitConfigs = iTUnitConfigService.selectTUnitConfigList(tUnitConfigRequest);
        TUnitConfig tUnitConfig = tUnitConfigs.get(0);
        return jyService.selectEventSubscriptionsForUnitCodes(tUnitConfig);
    }

    /**
     * 取消订阅
     */
    @DeleteMapping("/eventSubscriptions")
    public String deleteEventSubscriptions(@RequestBody Map param) {
        return jyService.deleteEventSubscriptions(param);
    }

    /**
     * 取消订阅
     * 修改时间 24/10/25
     * 修改原因 新增权限管理
     */
    @DeleteMapping("/eventSubscriptions/delete")
    public String deleteEventSubscriptionsForUnitCodes(@RequestBody Map param) {
        Long unitCode =(Long)param.get("unitCode");
        TUnitConfig tUnitConfigRequest = new TUnitConfig();
        tUnitConfigRequest.setUnitCode(unitCode);
        List<TUnitConfig> tUnitConfigs = iTUnitConfigService.selectTUnitConfigList(tUnitConfigRequest);
        TUnitConfig tUnitConfig = tUnitConfigs.get(0);
        return jyService.deleteEventSubscriptionsForUnitCodes(tUnitConfig,param);
    }
    /**
     * 图片url地址转换
     */
    @PostMapping("/picAddrTrans")
    public String picAddrTransx(@RequestBody List<String> addrList) {
        return jyService.picAddrTrans(addrList);
    }

    /**
     * 获取通道列表
     */

    @GetMapping("/getChannelListWithDevice")
    public String getChannelListWithDevice() {
        return jyService.getChannelListWithDevice();
    }
    /**
     * 获取视频流URL 弃用
     */

    @RequestMapping(value = "/getLiveStreamUrl", method = {RequestMethod.GET, RequestMethod.POST})
    public List<Map<String,Object>> getLiveStreamUrl(@RequestBody StreamRequest streamRequest) {

        if(JY_SWITCH.equals("true")){

            List liveStreamUrlApp =new ArrayList();
            HashMap<Object, Object> map = new HashMap<>();
            Map liveStreamUrlAppV2 = jyService.getLiveStreamUrlAppV2( streamRequest);
            Map<String,Object> data = (Map<String,Object>)liveStreamUrlAppV2.get("data");
            String  streamAddress= (String)data.get("streamAddress");
            String  apeId = (String)data.get("apeId");
            map.put("rtsp",streamAddress);
            map.put("apeId",apeId);
            liveStreamUrlApp.add(map);
            return liveStreamUrlApp;

        }else {

            List<Map<String,Object>> list= new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            map.put("rtsp","http://172.16.129.236:8060/api/vms/v1/webuas/media-server/0/livestream.m3u8?id=wJKV0Gr9k6jgw5ghlPu1&camera_id=10000000001311000001&stream_type=0&keep_alive=99999&port_type=0&h265_muxer_type=0&visit_ip=172.16.129.238");
            map.put("apeId","10000000001311000001");
            list.add(map);
            return list;

        }

    }

    /**
     * 获取视频流URL 弃用
     */
    @GetMapping("/getLiveStreamUrl/App")
    public List<Map<String,Object>> getLiveStreamUrlApp(@RequestParam Map param) {
        System.out.println(param.toString());
        if(JY_SWITCH.equals("true")){
            return jyService.getLiveStreamUrlApp(param);

        }else {

            List<Map<String,Object>> list= new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            map.put("rtsp","http://172.16.129.236:8060/api/vms/v1/webuas/media-server/0/livestream.m3u8?id=wJKV0Gr9k6jgw5ghlPu1&camera_id=10000000001311000001&stream_type=0&keep_alive=99999&port_type=0&h265_muxer_type=0&visit_ip=172.16.129.238");
            map.put("apeId","10000000001311000001");
            list.add(map);
            return list;

        }

    }

    /**
     * 获取视频流URL 版本V2 在用
     */
    @RequestMapping(value = "/getLiveStreamUrl/v2", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult getLiveStreamUrlAppV2(@RequestBody StreamRequest streamRequest) {
        if (JY_SWITCH.equals("true")) {
            return AjaxResult.success(jyService.getLiveStreamUrlAppV2(streamRequest));
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("apeId", "10000000001311000001");
            map.put("streamAddress", "http://172.16.129.236:8060/api/vms/v1/webuas/media-server/0/livestream.m3u8?"
                    + "id=wJKV0Gr9k6jgw5ghlPu1"
                    + "&camera_id=10000000001311000001"
                    + "&stream_type=0"
                    + "&keep_alive=99999"
                    + "&port_type=0"
                    + "&h265_muxer_type=0"
                    + "&visit_ip=172.16.129.238");
            map.put("streamMode", "HLS");

            return AjaxResult.success(map);
        }
    }
    /**
     * 获取回放流URL（方案一） 弃用
     */
    @GetMapping("/getLiveStreamUrl/replay")
    public List<Map<String,Object>> getLiveStreamUrlReplay(@RequestParam Map param) {
        return jyService.getLiveStreamUrlReplay(param);
    }
    /**
     * 获取回放流URL 版本V2 在用
     */
    @GetMapping("/getLiveStreamUrl/replay/v2")
    public Map<String,Object> getLiveStreamUrlReplayV2(@RequestParam Map param) {
        return jyService.getLiveStreamUrlReplayV2(param);
    }
    /**
     * 获取告警回放录像URL
     */
    @GetMapping("getPlayRecordUrl")
    public String getPlayRecordUrl(String channel_code, String event_time, String event_id) {
        return jyService.getPlayRecordUrl(channel_code, event_time, event_id);
    }

    /**
     * 获取org_index和org_name
     */
    @GetMapping("getIndexAndName")
    public String getIndexAndName() {
        return jyService.getIndexAndName();
    }
    /**
     * 新增硬盘录像机
     */
    @PostMapping("addDVR")
    public String addDVR(@RequestBody Map param){
        return jyService.addDVR(param);
    }
    /**
     * 查询视频质量诊断结果
     */
    @GetMapping("getVideoDiagnosisResults")
    public String getVideoDiagnosisResults(String channel_name,  String start_time, Integer page_num, Integer page_size) {
        return jyService.getVideoDiagnosisResults(channel_name, start_time,  page_num, page_size);
    }
    /**
     * 最后一次诊断结果查询
     */
    @PostMapping("getLastVideoDiagnosisResult")
    public String getLastVideoDiagnosisResult(@RequestBody Map param) {
        return jyService.getLastVideoDiagnosisResult(param);
    }
    /**
     * 查询已安装的算法信息
     */
    @GetMapping("installed-algos")
    public String getInstalledAlgorithms() {
        return  jyService.getInstalledAlgorithms();
    }

    /**
     * 查询视频分析任务
     */
    @GetMapping("video-analysis-tasks")
    public String getVideoAnalysisTasks() {
        return  jyService.getVideoAnalysisTasks();
    }
    /**
     * 统计监控点在线率
     */
    @GetMapping("count-online-rate")
    public String countOnlineRate(String pageSize, String pageNum) {
        return jyService.countOnlineRate(pageSize, pageNum);
    }
    /**
     * 统计监控点达标率
     */
    @GetMapping("count-compliance-ratio")
    public String countComplianceRatio(String pageSize, String pageNum, String queryDate) {
        return jyService.countComplianceRatio(pageSize, pageNum, queryDate);
    }

    /**
     * 批量添加诊断计划
     *
     * @param requestBody 包含诊断计划信息的请求体
     * @return 返回操作结果的 AjaxResult
     */
    @PostMapping("batchAddDiagnosisPlan")
    public AjaxResult batchAddDiagnosisPlan(@RequestBody DiagnosisPlanRequestBody requestBody) {
        return jyService.batchAddDiagnosisPlan(requestBody);
    }

    /**
     * 获取最终视频流URL互联网调用 -在用-
     */
//    @GetMapping("/getLiveStreamUrl/final")
    @RequestMapping(value = "/getLiveStreamUrl/final", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> getLiveStreamUrlFinalAppV2(@RequestBody StreamRequest streamRequest) {
        return jyService.getLiveStreamUrlFinalAppV2(streamRequest);
    }

    /**
     * 获取回放流URL 版本V2 -在用-
     */
    @GetMapping("/getLiveStreamUrl/replay/final")
    public Map<String,Object> getLiveStreamUrlReplayFinal(@RequestParam Map param) {
        return jyService.getLiveStreamUrlReplayFinal(param);
    }

    /**
     * 获取录像下载 URL-
     */
    @GetMapping("/api/vms/v2/webuas/replay/stream/url")
    public Map<String,Object> getReplayStreamUrl(@RequestParam String channel_code, @RequestParam String start_time,
                                                 @RequestParam String end_time) {
        return jyService.getReplayStreamUrl(channel_code,start_time,end_time);
//        return null;
    }

    /**
     * 获取录像下载 URL-
     */
    @GetMapping("/api/vms/v2/webuas/replay/stream/urlv2")
    public Map<String,Object> getReplayStreamUrlv2(@RequestParam String channel_code, @RequestParam String start_time,
                                                 @RequestParam String end_time) {
        return jyService.getReplayStreamUrlv2(channel_code,start_time,end_time);
//        return null;
    }

    /**
     * 获取录像下载 -
     */
    @GetMapping("/api/vms/v2/webuas/record/stream/download")
    public Map<String,Object> getRecordStreamDownload(@RequestParam String channel_code, @RequestParam String start_time,
                                                      @RequestParam String end_time,@RequestParam String output_file) {
        return jyService.getRecordStreamDownload(channel_code,start_time,end_time, output_file);
//        return null;
    }

}
